<template>
  <el-form ref="form" :model="writerInfo" label-width="80px">
    <el-form-item label="读者名称">
      <el-input v-model="writerInfo.writerName"></el-input>
    </el-form-item>
    <el-form-item label="读者电话">
      <el-input v-model="writerInfo.writerTel"></el-input>
    </el-form-item>
    <el-form-item label="读者密码">
      <el-input v-model="writerInfo.writerPassword"></el-input>
    </el-form-item>
    <el-form-item label="读者邮箱">
      <el-input v-model="writerInfo.writerEmail"></el-input>
    </el-form-item>
    <el-form-item label="读者余额">
      <el-input v-model="writerInfo.writerBalance"></el-input>
    </el-form-item>
    <el-form-item label="月票数">
      <el-input v-model="writerInfo.writerTicket"></el-input>
    </el-form-item>
    <el-form-item label="读者头像">
      <el-upload
        class="avatar-uploader"
        action="http://localhost:8888/writer/uploadPic"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="writerInfo.writerPic" :src="writerInfo.writerPic" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即修改</el-button>
      <el-button>取消</el-button>
    </el-form-item>

  </el-form>
</template>

<script>
  import axios from 'axios';
  export default {
    name: "writerInfo",
    data(){
      return{
        writerInfo:{
          writerName:'',
          writerTel:'',
          writerPassword:'',
          writerEmail:'',
          writerBalance:'',
          writerTicket:'',
          writerPic:''

        }
      }
    },
    methods:{
      findWriterById(id){
        axios.post('http://localhost:8888/writer/findById',{id:id}).then(res=>{
          if(res.data.code==0){
            this.writerInfo=res.data.data;
          }
        })
      },
      onSubmit() {
        axios.post('http://localhost:8888/writer/update',this.writerInfo).then(res=>{
          if(res.data.code==0){
            this.$router.push("3");
          }
        })

      },
      handleAvatarSuccess(res) {
        console.log(res);
        this.writerInfo.writerPic=res;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    },
    mounted() {
      var id = this.$route.params.id;
      if(id!=null){
        this.findWriterById(id);
      }

    }
  }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
